package com.timothy.junk.tfund.activity;

import java.util.List;

import com.markupartist.android.widget.PullToRefreshListView;
import com.sina.oefund.SinaOEFundClient;
import com.sina.stock.SinaStockClient;
import com.timothy.junk.R;
import com.timothy.junk.ActivityManage;
import com.timothy.junk.adapter.TFundAdaptor;
import com.timothy.junk.calculate.TFundCalculator;
import com.timothy.junk.common.parse.TFundParse;
import com.timothy.junk.common.util.NetWorkUtil;
import com.timothy.junk.tfund.bean.TFund;
import com.timothy.junk.tfund.bean.TFundResult;
import android.app.ListActivity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

public class TFundList extends ListActivity {
	public static final String LOG_TAG = "CBondList";

    Context mContext;
    TFundAdaptor adaptor ;
    
    List<TFund> tFundList;
    List<TFundResult> tFundResultList;
    
    SinaStockClient mClient;
    SinaOEFundClient fClient;
    
    public void loadData(){
    	tFundList = TFundParse.getTFundList(mContext);
    	TFundCalculator cal = new TFundCalculator(tFundList);
        cal.initial();
        cal.getPriceList(SinaStockClient.getInstance());
        cal.getValueList(SinaOEFundClient.getInstance());
        cal.calculate();
        tFundResultList = cal.gettFundResultList();
    }
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tfund_list);
        
        mContext = getApplicationContext();
        ActivityManage.add(this);
        
        loadData();
        
        // Set a listener to be invoked when the list should be refreshed.
        ((PullToRefreshListView) getListView()).setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new GetDataTask().execute();
            }
        });

        if(NetWorkUtil.isNetworkAvailable(mContext)){
        	new GetDataTask().execute();
        }else{
        	Toast.makeText(mContext, "Your network is not available!", Toast.LENGTH_LONG).show();
        }
        
        ((PullToRefreshListView) getListView()).setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Log.i(LOG_TAG,"-----onItemClick()-----");
//				Intent intent=new Intent(mContext, GFundDetail.class);
//				GFundBean sBean = gFundList.get(arg2-1);
//				intent.putExtra("index",sBean.getIndex());
//				startActivity(intent);
			}
        });
    }
    
    private class GetDataTask extends AsyncTask<Void, Void, String[]> {
        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(2000);
                loadData();
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(String[] result) {
            super.onPostExecute(result);
            if(tFundResultList!=null&&tFundResultList.size()>0){
                adaptor = new TFundAdaptor(mContext, tFundResultList);
                setListAdapter(adaptor); 
                ((PullToRefreshListView) getListView()).onRefreshComplete();
            }else{
            	Toast.makeText(mContext, "Can not get any data!", Toast.LENGTH_LONG).show();
            }
        }
    }
}
